package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.domain.Position;
import com.woniu.mapper.PositionDetailsMapper;
import com.woniu.mapper.PositionMapper;
import com.woniu.service.PositionDetailsService;
import com.woniu.vo.CompanyPositionSort;
import com.woniu.vo.PageVo;
import com.woniu.vo.PositionDetails;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

@Service
public class PositionDetailsServiceImpl extends ServiceImpl<PositionDetailsMapper, PositionDetails> implements PositionDetailsService {

    @Resource
    private PositionDetailsMapper positionDetailsMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private PositionMapper positionMapper;


    @Override
    public IPage<PositionDetails> getHotPositions(PageVo pageVo) {

//        //从数据库中查询热点信息
//        Page<PositionDetails> page = new Page<>(pageVo.getCurrent(), pageVo.getSize());
//        QueryWrapper<PositionDetails> ew = new QueryWrapper<>();
//        ew.eq("c.status", "已通过");
//        ew.eq("p.statusc", "发布");
//        IPage<PositionDetails> positionDetails = positionDetailsMapper.getPositionDetails(page, ew);
//
//        if (!ObjectUtils.isEmpty(positionDetails)) {
//            List<PositionDetails> records = positionDetails.getRecords();
//            if (!ObjectUtils.isEmpty(records)) {
//                records.forEach(record -> {
//                    //往redis中新增一些数据
//                    stringRedisTemplate.opsForHash().put("position1:id:" + record.getPositionId(), "Position1VisitCount", record.getPositionVisitCount().toString());
//                    stringRedisTemplate.opsForHash().put("position1:id" + record.getPositionId(), "Position1Hot", record.getHot());
//                    //用来做排行榜的
//                    stringRedisTemplate.opsForSet().add("position1:ids", record.getPositionId().toString());
//                });
//            }
//
//            Set<String> ids = stringRedisTemplate.opsForSet().members("position1:ids");
//            ArrayList<CompanyPositionSort> sorts = new ArrayList<>();
//
//            ArrayList<PositionDetails> positionDetails1 = new ArrayList<PositionDetails>();
//
//            ids.forEach(id -> {
//                Object position1VisitCount = stringRedisTemplate.opsForHash().get("position1:id:" + id, "Position1VisitCount");
//                String position1Hot = (String) stringRedisTemplate.opsForHash().get("position1:id:" + id, "Position1Hot");
//                if (!ObjectUtils.isEmpty(position1Hot) && position1Hot.equals("平台推送")) {
//                    positionDetails1.add(positionDetailsMapper.selectById(id));
//                    return;
//                }
//                sorts.add(CompanyPositionSort.builder().positionId(Integer.valueOf(id))
//                        .positionVisitCount(Integer.valueOf(position1VisitCount.toString()))
//                        .build());
//                //可以定时更新数据库
//                Position entity = new Position();
//                entity.setPositionId(Integer.valueOf(id));
//                entity.setVisitCount(Integer.valueOf(position1VisitCount.toString()));
//                positionMapper.updateById(entity);
//            });
//
//            Collections.sort(sorts);
//            stringRedisTemplate.delete("positions:sortId");
//            int count = 0;
//            int size = positionDetails1.size();
//            for (int i = 0; i < sorts.size() + size; i++) {
//                if (++count > 10) break;
////                positionDetails1.add(positionDetailsMapper.selectById(sorts.get(i).getPositionId()));
//                stringRedisTemplate.opsForList().rightPush("positions:sortId", (sorts.get(i).getPositionId().toString()));
//            }
//            positionDetails.setRecords(positionDetails1);
//            return positionDetails;
//        }

        return null;
    }
}
